﻿using System;
using System.IO;
using Xpedite.Extensions;
using Xpedite.Resources.Definitions;
using Xpedite.WebAbstraction;

/* Copyright (c) 2010 CBaxter
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), 
 * to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, 
 * and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 
 * IN THE SOFTWARE. 
 */

namespace Xpedite.Resources
{
  internal interface IResourceUrlGenerator
  {
    IResourceUrl GenerateUrlFor(String path);
    IResourceUrl GenerateUrlFor(IResourceUrl resourceReference);
    IResourceUrl GenerateUrlFor(IResourceDefinition resourceDefinition);
    IResourceUrl GenerateUrlFor(IResourceDefinition resourceDefinition, ResourceType resourceTypeOverride);
  }

  internal class ResourceUrlGenerator : IResourceUrlGenerator
  {
    private readonly Func<IWebRequest> _requestRetriever;
    private readonly String _rootVirtualPath;

    public ResourceUrlGenerator(Func<IWebRequest> requestRetriever, String rootVirtualPath) 
    {
      Verify.NotNull(rootVirtualPath, "rootVirtualPath");
      Verify.NotNull(requestRetriever, "requestRetriever");
      Verify.False(rootVirtualPath.EndsWith("/"), "rootVirtualPath", "Value must not end with a trailing slash '/'.");

      _rootVirtualPath = rootVirtualPath;
      _requestRetriever = requestRetriever;
    }

    public IResourceUrl GenerateUrlFor(String path)
    {
      return new ResourceUrl(_requestRetriever.Invoke(), path);
    }

    public IResourceUrl GenerateUrlFor(IResourceUrl resourceUrl)
    {
      Verify.NotNull(resourceUrl, "resourceUrl");

      String name = Path.GetFileName(resourceUrl.VirtualPath);
      String hexHashCode = resourceUrl.VirtualPath.GetHexHashCode();

      return new ResourceUrl(_requestRetriever.Invoke(), String.Format("{0}/xpedite.{1}.{2}.xcr", _rootVirtualPath, hexHashCode, name));
    }

    public IResourceUrl GenerateUrlFor(IResourceDefinition resourceDefinition)
    {
      return GenerateUrlFor(resourceDefinition, resourceDefinition.ResourceType);
    }

    public IResourceUrl GenerateUrlFor(IResourceDefinition resourceDefinition, ResourceType resourceTypeOverride)
    {
      Verify.NotNull(resourceDefinition, "resourceDefinition");

      Int32 urlHashCode;
      Int32 lastModifiedHashCode;

      unchecked
      {
        urlHashCode = (397 * resourceDefinition.ReferenceName.GetHashCode());
        lastModifiedHashCode = (397 * (resourceTypeOverride.GetHashCode() + 1));

        foreach (var resource in resourceDefinition.Resources)
        {
          urlHashCode = (397 * urlHashCode) ^ resource.ResourceUrl.VirtualPath.GetHashCode();
          lastModifiedHashCode = (397 * lastModifiedHashCode) ^ resource.Resource.LastModified.GetHashCode();
        }
      }

      String name = resourceDefinition.ReferenceName;
      String type = MapResourceTypeToExtension(resourceTypeOverride);
      String hexHashCode = urlHashCode.GetHexHashCode() + lastModifiedHashCode.GetHexHashCode();

      return new ResourceUrl(_requestRetriever.Invoke(), String.Format("{0}/xpedite.{1}.{2}.{3}.xcr", _rootVirtualPath, hexHashCode, name, type));
    }

    private String MapResourceTypeToExtension(ResourceType resourceType)
    {
      switch (resourceType)
      {
        case ResourceType.Css: return "css";
        case ResourceType.Gif: return "gif";
        case ResourceType.JavaScript: return "js";
        case ResourceType.Jpeg: return "jpg";
        case ResourceType.Png: return "png";
        default: return resourceType.ToString();
      }
    }
  }
}
